﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QuanLyThuoc
{
    class DanhSachKhachHang
    {
        int n;
        static List<KhachHang> DSKH;

        //khoi tao class
        public DanhSachKhachHang()
        {
            DSKH = new List<KhachHang>();
        }

        //them moi khach hang
        public void ThemDSKH()
        {
            Console.Write("\nSo khach hang them vao danh sach: ");
            while (int.TryParse(Console.ReadLine(), out n) == false)
            {
                Console.WriteLine("Nhap sai! Nhap lai!");
            }
            for (int i = 0; i < n; i++)
            {
                KhachHang KH = new KhachHang();
                KH.NhapKH();
                DSKH.Add(KH);
                KH.ghiFile();
            }
        }

        //tim khach hang theo ma
        public static KhachHang TimTheoMa(string matim)
        {
            foreach(KhachHang KH in DSKH)
            {
                if (matim == KH.MaKH)
                    return KH;
            }
            return null;
        }

        //tim khach hang theo ten
        public List<KhachHang> TimTheoTen(string tentim)
        {
            List<KhachHang> dsKH = new List<KhachHang>();
            foreach (KhachHang khach in DSKH)
            {
                if (khach.HoTen.Contains(tentim))
                {
                    dsKH.Add(khach);
                }
            }
            return dsKH;
        }

        //tim khach hang theo dia chi
        public List<KhachHang> TimTheoDiaChi(string dctim)
        {
            List<KhachHang> DS = new List<KhachHang>();
            foreach (KhachHang KH in DSKH)
            {
                if (KH.DiaChi.Contains(dctim))
                {
                    DS.Add(KH);
                }
            }
            return DS;
        }

        //tim kiem khach hang theo dieu kien lua chon
        public void TimKiem()
        {
            int x = -1;
            Console.WriteLine("Nhap: \n1: Tim theo ma \n2: Tim theo ten \n3: Tim theo dia chi");
            while (int.TryParse(Console.ReadLine(), out x) == false)
            {
                Console.WriteLine("Nhap sai!");
            }
            switch (x)
            {

                case 1:
                    Console.Write("\nNhap ma khach hang can tim: ");
                    string ma = Console.ReadLine();
                    KhachHang KHtim = TimTheoMa(ma);
                    if (KHtim != null)
                    {
                        Console.Write("\n\tMa khach hang".PadRight(5));
                        Console.Write("\tHo ten".PadRight(5));
                        Console.Write("\tDia chi".PadRight(5));
                        Console.Write("\tDien thoai".PadRight(5));
                        KHtim.XuatKH();
                    }
                    else
                    {
                        Console.WriteLine("Khong tim thay!");
                    }
                    break;

                case 2:
                    Console.Write("\nNhap ten can tim: ");
                    string ten = Console.ReadLine();
                    List<KhachHang> TimTen = TimTheoTen(ten);
                    if (TimTen.Count > 0)
                    {
                        Console.Write("\tMa khach hang \tHo ten \tDia chi \tDien thoai");
                        foreach (KhachHang dt in TimTen)
                        {
                            dt.XuatKH();
                        }
                    }
                    else
                        Console.WriteLine("Khong tim thay!");
                    break;

                case 3:
                    {
                        Console.Write("\nNhap dia chi can tim: ");
                        string diachi = Console.ReadLine();
                        List<KhachHang> DSTimDC = TimTheoDiaChi(diachi);
                        if (DSTimDC.Count > 0)
                        {
                            Console.Write("\tMa khach hang \tHo ten \tDia chi \tDien thoai");
                            foreach (KhachHang dt in DSTimDC)
                            {
                                dt.XuatKH();
                            }
                        }
                        else
                            Console.WriteLine("Khong tim thay!");
                    }
                    break;
            }
        }

        //xoa 1 khach hang
        public bool XoaKH(string maxoa)
        {
            KhachHang DtXoa = TimTheoMa(maxoa);
            if (DtXoa != null)
            {
                DSKH.Remove(DtXoa);
                n--;
                return true;
            }
            return false;
        }

        //sua thong tin khach hang
        public bool SuaDSKH(string masua)
        {
            KhachHang SuaDS = TimTheoMa(masua);
            int x = -1;
            while (x != 0)
            {
                Console.Write("\nChon chuc nang can sua: \n\t1: Ten \n\t2: Dia chi \n\t3: Dien thoai\n\t0: Quay lai");
                x = int.Parse(Console.ReadLine());
                switch (x)
                {
                    case 1:
                        Console.Write("\nNhap ten moi: ");
                        SuaDS.HoTen = Console.ReadLine();
                        break;
                    case 2:
                        Console.Write("\nNhap dia chi moi: ");
                        SuaDS.DiaChi = Console.ReadLine();
                        break;
                    case 3:
                        Console.Write("\nNhap so dien thoai moi: ");
                        SuaDS.SoDT = Console.ReadLine();
                        break;
                }
                return true;
            }
            return false;
        }

        //xuat danh ds khach hang
        public void XuatDSKH()
        {
            foreach (KhachHang DSXuat in DSKH)
            {
                DSXuat.XuatKH();
            }
        }

        //doc file
        public void docFile()
        {
            FileStream file = new FileStream("DSKhachHang.txt", FileMode.Open);
            BinaryReader br = new BinaryReader(file);
            while (br.PeekChar() != -1)
            {
                KhachHang KH = new KhachHang();
                KH.MaKH = br.ReadString();
                KH.HoTen = br.ReadString();
                KH.DiaChi = br.ReadString();
                KH.SoDT = br.ReadString();
                DSKH.Add(KH);
            }
            br.Close();
            file.Close();
        }
    }
}
